var cs=10;
var hosturl=window.location.hostname;
var websocket;
var token;
var room;
var index;
var timewc=0;
var bgmp3open;
var mp3open;
var overzt=1;
if(!localStorage.bgmp3open){
    bgmp3open=1;
}
else{
    bgmp3open=localStorage.bgmp3open;
}
if(!localStorage.mp3open){
    mp3open=1;
}
else{
    mp3open=localStorage.mp3open;
}
function connect(n){
  if(overzt==0){
    load('hide');
    return false;
  }
  if(websocket)websocket.close();
    if(cs>0){
      cs=cs-1;
      websocket = new WebSocket("ws://"+hosturl+":"+n);
      websocket.onmessage = function(event) {
        cs=10;
        zdata=JSON.parse(event.data);
        //console.log(zdata);
        window[zdata.act](zdata.msg);
      };
      websocket.onopen=function(e){
            load('hide');
            if(!token){
                token=localStorage.token;
            }
            send('init',{token:token,room:room});
      }
      websocket.onclose=function(e){
            if(overzt==0){
                return false;
                load('hide');
              }
            load('show');
            //prompt('网络不给力,正在努力链接服务器!');
            websocket.close(); //关闭TCP连接
            cs=cs-1;
            connect(n);
      };
    }
    else{
      load('hide');
      //prompt('已经于服务器断开,请刷新重试!');
      setTimeout("$('body').hide()",3000);
    }
}
function gologin(){
    location.href="/";
}
function gxtoken(data){
    localStorage.token=data;
}
//加载中
function load(s) {
    var Img = ""
    var html = "<div class=\"load\"><img src=" + Img + " /></div>";
    if (s == 'show') {
        $('body').append(html);
    }
    if (s == 'hide') {
        $('.load').remove();
    }
    maik(s);
}
//遮罩
function maik(s) {
    var hei = $(window).height();
    var wid = $(window).width();
    var html = "<div class=\"maik\" style='height:"+hei+"px;width:"+wid+"px;' onclick=\"Center($('.noticewi'), 'hide')\"></div>";
    if (s == 'show') {
        if ($(".maik").length <= 0) {
            $('body').append(html);
        }
    }
    if (s == 'hide') {
        $('.maik').remove();
    }
}
//获取页参数
function Request(m) {
    var sValue = location.search.match(new RegExp("[\?\&]" + m + "=([^\&]*)(\&?)", "i"));
    return sValue ? sValue[1] : sValue;
}
function prompt(txt) {
    var html = "<div class=\"prompt\"><span>" + txt + "</span></div>";
    var Bw = $(window).width();
    var Bh = $(window).height();
    $('body').append(html);
    var Pw = $('.prompt').width();
    var Ph = $('.prompt').height();
    $('.prompt').css({
        "top": Bh / 2 - Ph / 2,
        "left": Bw / 2 - Pw / 2
    });
    setTimeout(function () {
        $('.prompt').remove();
    }, 2000);
}

//记录登陆凭证
function login(data){
    localStorage.userid=data.uid;
    localStorage.salt=data.salt;
}
//销毁登陆凭证
function loginout(msg){
    if(msg){
        prompt(msg);
        setTimeout("$('body').hide()",3000);
    }
    localStorage.token='';
    websocket.close();
}

function send(act,data){
    if(!data){
        data={};
    }
    var fs = data;
    fs.act=act;
    var jsonStr = JSON.stringify(fs);
    if(typeof(websocket)=='undefined'){
        prompt('与服务器端口链接，请刷新重试');
        return false;
    }
    websocket.send(jsonStr);
}

//标题
function Title(title) {
    document.title = title;
}
function attr(data){
    $('#'+data.id).attr(data.wz,data.nr);
}
function html(data){
    $('#'+data.id).html(data.html);
}

function addid(data){
    $('#'+data.id).addClass(data.html);
}


function removeid(data){
    $('#'+data.id).removeClass(data.html);
}


function value(data){
    $('#'+data.id).val(data.html);
}
function value2(data){
    $(''+data.id).val(''+data.html);
}
function append(data){
    $('#'+data.id).append(data.html);
}
function jsdata(data){
    eval(data);
}
function active(data){
    $('#'+data.id).addClass(data.html).siblings().removeClass(data.html);
} 

function showid(data){
    $('#'+data.id).show();
}
function hideid(data){
    $('#'+data.id).hide();
}

function goroom(data){
    location.href="/index.php/portal/index/room.html?room="+data.room;
}

function gxindex(data){
    index=data;
}
function timewcgx(data){
    timewc=Math.ceil(new Date()/1000)-data;
    //console.log(timewc);
    setTimeout('send("timegx",{})',5000);
}


function WeChat(url,title,img,desc){
     $.get("/index.php/portal/index/getconfig/",{url:window.location.href},function(data) {
        wx.config(data);
        wx.ready(function () {
        // 在这里调用 API
        wx.onMenuShareTimeline({  //例如分享到朋友圈的API 
        title: title, // 分享标题
        link: url, // 分享链接
        imgUrl: img, // 分享图标
        success: function () {
        },
        cancel: function () {
        // 用户取消分享后执行的回调函数
        }
        });
 
        wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: url, // 分享链接
        imgUrl: img, // 分享图标
        type: '', // 分享类型,music、video或link，不填默认为link
        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
        success: function () {
        },
        cancel: function () {
        // 用户取消分享后执行的回调函数
        }
        });
        wx.error(function (res) {
         //打印错误消息。及把 debug:false,设置为debug:ture就可以直接在网页上看到弹出的错误提示
        });
        });
    },
    "json"
    );
}